package com.frxuexi.park2304.user

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.LoginBean
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.park2304.R
import com.frxuexi.park2304.databinding.ActivityRegisterBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/app/RegisterActivity")
@AndroidEntryPoint
class RegisterActivity : BaseActivity<ActivityRegisterBinding,UserIntent,UserViewModel>() {
    override val binding: ActivityRegisterBinding by lazy { ActivityRegisterBinding.inflate(layoutInflater) }
    override val vm: UserViewModel by viewModels<UserViewModel>()
    var code=""
    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        val id = it.data as Long
                        Log.i("lr",id.toString())
                        if(id>0){
                            ToastUtils.showLong("注册成功")
                            finish()
                        }
                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        binding.btnRegister.setOnClickListener {
            val uname = binding.registerUname.text.toString()
            val pass = binding.registerPass.text.toString()
            val pass2 = binding.registerPass2.text.toString()
            val code = binding.registerCode.text.toString()

            if(code!=this.code){
                ToastUtils.showLong("验证码不正确")
                return@setOnClickListener
            }

            if(pass!=pass2){
                ToastUtils.showLong("两次密码不一致")
                return@setOnClickListener
            }

            vm.sendIntent(UserIntent.register(LoginBean(null,uname,pass)))
        }

        binding.registerSendcode.setOnClickListener {
            code=(1000..9999).random().toString()
            ToastUtils.showLong(code)
        }
    }

}